from django.db import models
from django.contrib.auth.models import AbstractUser


# 用户表
class User(AbstractUser):
    mobile = models.CharField(max_length=11)

    class Meta:
        db_table = 'user'

    def __str__(self):
        return self.username


class BaseModel(models.Model):
    create_time = models.DateTimeField(auto_now_add=True)
    update_time = models.DateTimeField(auto_now=True)

    class Meta:
        abstract = True


# 三方登录表
class SFlogin(BaseModel):
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    webfrom = models.CharField(max_length=20)  # 微信，qq
    uid = models.CharField(max_length=50)
    token = models.CharField(max_length=50)

    class Meta:
        db_table = 'sflogin'


# 区域表
class Area(models.Model):
    name = models.CharField(max_length=30)
    pid = models.ForeignKey('self', on_delete=models.SET_NULL, null=True, related_name='son')

    class Meta:
        db_table = 'tb_areas'


# 地址表
class Address(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    provice = models.ForeignKey(Area, on_delete=models.CASCADE, related_name='provice')
    city = models.ForeignKey(Area, on_delete=models.CASCADE, related_name='city')
    area = models.ForeignKey(Area, on_delete=models.CASCADE, related_name='area')
    linkman = models.CharField(max_length=30)
    tel = models.CharField(max_length=20)
    address = models.CharField(max_length=255)
    email = models.EmailField()
    mobile = models.CharField(max_length=11)
    is_default = models.BooleanField(default=0)

    class Meta:
        db_table = 'address'


# 商品分类表
class Cate(BaseModel):
    name = models.CharField(max_length=50)
    pic = models.ImageField()
    pid = models.ForeignKey('self', on_delete=models.CASCADE, related_name='son')
    level = models.IntegerField()
    topid = models.IntegerField()

    class Meta:
        db_table = 'cate'


# 商品表
class Goods(BaseModel):
    name = models.CharField(max_length=50)
    pic = models.ImageField()
    cate = models.ForeignKey(Cate, on_delete=models.CASCADE, related_name='goods')
    price = models.DecimalField(max_digits=7, decimal_places=2)
    level = models.IntegerField()
    topid = models.IntegerField()
    store = models.IntegerField(default=9999)
    sales = models.IntegerField(default=0)
    tcomment = models.IntegerField(default=0)
    content = models.TextField()
    service = models.CharField(max_length=255)
    spu = models.CharField(max_length=200)
    is_hot = models.BooleanField(default=False)
    is_new = models.BooleanField(default=False)
    is_home = models.BooleanField(default=False)
    is_del = models.BooleanField(default=False)
    users = models.ManyToManyField(User)  # 收藏

    class Meta:
        db_table = 'goods'


# 订单表
class Orders(BaseModel):
    PAY_TYPE = (
        ('1', '支付宝'),
        ('2', '微信'),
        ('3', '货到付款'),
    )
    order_no = models.CharField(max_length=100, unique=True)
    tmoney = models.DecimalField(max_digits=10, decimal_places=2)
    pay_type = models.IntegerField(choices=PAY_TYPE, default=1)  # 支付类型 1支付宝 2微信
    pay_status = models.IntegerField()  # 1未支付  2代发货
    transaction_no = models.CharField(max_length=100)  # 流水号
    user = models.ForeignKey(User, on_delete=models.CASCADE)

    class Meta:
        db_table = 'orders'


# 订单详情表
class OrderDetails(BaseModel):
    order_no = models.ForeignKey(Orders, to_field='order_no', on_delete=models.CASCADE)
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    goods = models.ForeignKey(Goods, on_delete=models.CASCADE)
    name = models.CharField(max_length=100)
    price = models.DecimalField(max_digits=7, decimal_places=2)
    count = models.IntegerField()
    pic = models.CharField(max_length=100)

    class Meta:
        db_table = 'orders_details'


# 广告表
class Ad(BaseModel):
    pic = models.ImageField()
    url = models.CharField(max_length=100)
    type = models.IntegerField(default=1)

    class Meta:
        db_table = 'ad'


# 新闻表
class News(BaseModel):
    title = models.CharField(max_length=100)
    content = models.TextField()

    class Meta:
        db_table = 'news'


# 评论表
class Comment(BaseModel):
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    goods = models.ForeignKey(Goods, on_delete=models.CASCADE)
    content = models.CharField(max_length=255)
    star = models.IntegerField(default=5)

    class Meta:
        db_table = 'comment'







